﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using RealState.BLL;
using RealState.BusinessObjects;
using System.Data;
using RealState.Utils;
using Geekees.Common.Controls;
using System.IO;

namespace RealState.W.AdminControl
{
    public partial class NewsCreate : System.Web.UI.UserControl
    {
        protected ILog log = LogManager.GetLogger(typeof(NewsCreate));
        private NewsBLL m_NewsBLL;
        private News m_NewsDO;
        private CategoriesBLL m_CategoriesBLL;
        private static int selectedNode = 0;
        private int catype = 0;
        public void bindData()
        {
            m_CategoriesBLL = new CategoriesBLL();

            try
            {
                DropDownList1.DataSource = m_CategoriesBLL.GetCategoriess().Where(c=>c.CategoryType==1 || c.CategoryType==5).ToList();
                DropDownList1.DataTextField = "TITLE";
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0,new ListItem("--Lựa chọn--","0"));
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
            finally
            {
                m_CategoriesBLL = null;
            }

        }

        
        private void bindThum()
        {
            ddlThumbnail.Items.Clear();
            string path = "~/upload/news/";
            string[] files = Directory.GetFiles(Server.MapPath(path));
            Action<string[], bool> action = delegate(string[] filde, bool forThumbs)
            {
                foreach (string str in files)
                {
                    string str5 = Path.GetExtension(str).ToLower();
                    string item = Path.GetFileName(str);
                    switch (str5)
                    {
                        case ".jpg":
                        case ".jpeg":
                        case ".png":
                        case ".gif":
                            if (forThumbs)
                            {
                                this.ddlThumbnail.Items.Add(item);
                            }
                            break;
                    }
                }
            };
            action(files, true);
        }

        protected void btnBindImagesThum_Click(object sender, EventArgs e)
        {
            string text = string.Empty;
            if (ddlThumbnail.SelectedIndex != -1)
            {
                text = ddlThumbnail.SelectedItem.Text.Trim();
            }
            bindThum();
            if (Session["uploaded_pic_thum"] != null)
            {
                string str3 = Session["uploaded_pic_thum"].ToString().Trim();
                Session["uploaded_pic_thum"] = null;
                if (Session["uploaded_pic_ext"] != null)
                {
                    if (Session["uploaded_pic_ext"].ToString().Trim().ToLower() == "thumb")
                    {
                        if (ddlThumbnail.Items.FindByText(str3) != null)
                        {
                            ddlThumbnail.SelectedIndex = -1;
                            ddlThumbnail.Items.FindByText(str3).Selected = true;
                        }

                    }

                    Session["uploaded_pic_ext"] = null;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindData();
                bindThum();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
                return;
           
            //if (selectedNode == null)
            //{
            //    JavaScript.Error("Bạn chưa chọn danh mục !", this.Page);
            //    return;
            //}
            m_NewsBLL = new NewsBLL();
            m_NewsDO = new News();
            try
            {
                m_NewsDO.Title = txtTitle.Text;
                m_NewsDO.ImgPath = ddlThumbnail.SelectedValue;
                m_NewsDO.IsActive = true;
                m_NewsDO.IsRemove = false;
                m_NewsDO.CreatedTime = DateTime.Now;
                m_NewsDO.Content = txtIntro.Text;
                m_NewsDO.Description = Server.HtmlEncode(txtContent.Text);
                m_NewsDO.CategoryId = Convert.ToInt32(DropDownList1.SelectedValue);
                if (Convert.ToInt32(DropDownList1.SelectedValue) == 2)
                    m_NewsDO.IsNews = true;
                else
                    m_NewsDO.IsNews = false;
                m_NewsDO.UserId = 1;
                int result = m_NewsBLL.AddNews(m_NewsDO);
                if (result > 0)
                {
                    JavaScript.Success("Thêm mới thành công", Page, Request.Url.ToString());
                }
                else
                    JavaScript.Error("Có lỗi xảy ra ", Page);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
            finally
            {
                m_NewsDO = null;
                m_NewsBLL = null;
            }
        }
    }
}